# -*- coding: utf-8 -*-

import os
import sys
import urllib
import urllib2
import sqlite3

from bs4 import BeautifulSoup

con=sqlite3.connect("")
def download(url,data):
    try:
        headers={
            'Host': 'api.douban.com',
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0',
            'Accept': 'text / html, application / xhtml + xml, application / xml;q = 0.9, * / *;q = 0.8',
            'Accept-Language': 'zh - CN, zh;q = 0.8, en - US;q = 0.5, en;q = 0.3',
            'Accept-Encoding': 'gzip, deflate, br',
            'Cookie': 'bid=n5s8ffCwX9A; __utma=30149280.624159412.1499933892.1499933892.1501737085.2; __utmz=30149280.1499933892.1.1.utmcsr=baidu|utmccn=(organic)|utmcmd=organic',
            'Connection': 'keep-alive',
            'Upgrade-Insecure-Requests': '1',
            'Cache-Control': 'max-age=0'
        }
        print 'begin download:',url
        request=urllib2.Request(url, data,headers)
        response=urllib2.urlopen(request)
        html=response.read()
    except urllib2.URLError as e:
        print 'download error:',url
        html = None
    except Exception as e:
        print 'download error:',url
        html = None
    return html

def get_all_books_by_tag(tag,start=0):
    url='https://api.douban.com/v2/book/search?tag='+tag+'&start='+str(start)+'&count=100'
    data=urllib.urlencode({"tag":tag,"start":start,"count":"100"})
    response=download(url,data)
    print data


if __name__ == '__main__':
    # if sys.argv[1] == '--help':
    #     print ''
    # elif sys.argv[1]:
    #     keyword=sys.argv[1]
    #     get_all_books_by_tag(keyword)
    keyword='金融'
    get_all_books_by_tag(keyword)


